﻿using System;


using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;

namespace MVCFoto.Web
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapRoute(
                   "User",                                             
                   "user/{userid}",                           
                   new { controller = "Account", action = "Index"}, 
                   new { userid = @"\d+" }
               );
            routes.MapRoute(
                   "Friends",                                             
                   "Friends",                           
                   new { controller = "Account", action = "Friends" }                  
               );
            routes.MapRoute(
                   "DeleteFriend",
                   "Friends/Remove/{userid}",
                   new { controller = "Account", action = "DeleteFriend" },
                   new { userid = @"\d+" }
               );
            routes.MapRoute(
                  "Photo",                                             
                  "Photo/{id}",                           
                  new { controller = "Photo", action = "Photo" }, 
                  new { id = @"\d+" }
              );
            routes.MapRoute(
                 "RotatePhoto",
                 "Photo/{id}/Rotate/{direction}",
                 new { controller = "Photo", action = "RotatePhoto" },
                 new { id = @"\d+", direction=@"(clockwise|anticlockwise)"}
             );
            routes.MapRoute(
                  "DeletePhoto",                                             
                  "Photo/{id}/Delete",                           
                  new { controller = "Photo", action = "DeletePhoto" }, 
                  new { id = @"\d+" }
              );
            //用户的所有相册
            routes.MapRoute(
                  "Photos",                                             
                  "Photos/{userid}",                           
                  new { controller = "Photo", action = "Index" }, 
                  new { userid = @"\d+" }
              );
            //批量编辑
            routes.MapRoute(
                 "EditPhotos",
                 "Photos/Edit/{ids}",
                 new { controller = "Photo", action = "EditPhotos" }
                 //new { userid = @"\d+" }
             );
            routes.MapRoute(
                  "PhotoAllSize",                                             
                  "Photo/{id}/all",                           
                  new { controller = "Photo", action = "AllSize" }, 
                  new { id = @"\d+" }
              );
            routes.MapRoute(
                "SetPhotoAsCover",
                "Photo/{id}/SetPhotoAsCover",
                 new { controller = "Photo", action = "SetPhotoAsCover" }, 
                 new { id = @"\d+" }
              );
            routes.MapRoute(
                "UploadToAlbum",
                "Album/{albumid}/Upload",
                 new { controller = "Photo", action = "Upload" }, 
                 new { albumid = @"\d+" }
              );
            routes.MapRoute(
                   "Album",                                             
                   "Album/{id}",                           
                   new { controller = "Photo", action = "Album" }, 
                   new { id = @"\d+" }
               );
            routes.MapRoute(
                  "AddAlbum",                                             
                  "Album/Add",                           
                  new { controller = "Photo", action = "AddAlbum" }                 
              );
                
            routes.MapRoute(
                  "EditAlbum",                                             
                  "Album/{id}/Edit",                           
                  new { controller = "Photo", action = "EditAlbum" }, 
                  new { id = @"\d+" }
              );

            routes.MapRoute(
                "Ajax",                                             
                "Ajax/{action}",                           
                new { controller = "Ajax", action = "Index"}  
            );

            routes.MapRoute(
               "Prompt",
               "Prompt",
               new { controller = "Prompt", action = "Index" }
           );         
            routes.MapRoute(
               "Account",
               "{action}",
               new { controller = "Account"}
           );
            routes.MapRoute(
                "Tag",
                "Tag/{userid}/{tag}",
                new { controller = "Photo", action = "TagedPhotos" },
                new { userid = @"\d+" }
            );
            routes.MapRoute(
                 "AddComment",
                 "Photo/{photoid}/AddComment",
                 new { controller = "Photo", action = "AddComment" },
                 new { photoid = @"\d+"}
             );
            routes.MapRoute(
                 "DeleteComment",
                 "Photo/{photoid}/Comment/{commentid}/Delete",
                 new { controller = "Photo", action = "DeleteComment" },
                 new { photoid = @"\d+", commentid = @"\d+" }
             );

            routes.MapRoute(
                "Feeds",
                "Feeds/{action}/{albumid}/{number}",
                new { controller = "Feeds", action = "Index",number=10},
                new { albumid = @"\d+", number = @"\d+" }
            );
            routes.MapRoute(
                "Default",                                             
                "{controller}/{action}/{id}",                           
                new { controller = "Home", action = "Index", id = "" }  
            );

        }

        protected void Application_Start()
        {
            RegisterRoutes(RouteTable.Routes);
        }

        void Application_BeginRequest(object sender, EventArgs e) {
        try
		{
			string session_param_name = "ASPSESSID";
			string session_cookie_name = "ASP.NET_SESSIONID";

			if (HttpContext.Current.Request.Form[session_param_name] != null)
			{
				UpdateCookie(session_cookie_name, HttpContext.Current.Request.Form[session_param_name]);
			}
			else if (HttpContext.Current.Request.QueryString[session_param_name] != null)
			{
				UpdateCookie(session_cookie_name, HttpContext.Current.Request.QueryString[session_param_name]);
			}
		}
		catch (Exception)
		{
			Response.StatusCode = 500;
			Response.Write("Error Initializing Session");
		}

		try
		{
			string auth_param_name = "AUTHID";
			string auth_cookie_name = FormsAuthentication.FormsCookieName;

			if (HttpContext.Current.Request.Form[auth_param_name] != null)
			{
				UpdateCookie(auth_cookie_name, HttpContext.Current.Request.Form[auth_param_name]);
			}
			else if (HttpContext.Current.Request.QueryString[auth_param_name] != null)
			{
				UpdateCookie(auth_cookie_name, HttpContext.Current.Request.QueryString[auth_param_name]);
			}

		}
		catch (Exception)
		{
			Response.StatusCode = 500;
			Response.Write("Error Initializing Forms Authentication");
		}
	}
	void UpdateCookie(string cookie_name, string cookie_value)
	{
		HttpCookie cookie = HttpContext.Current.Request.Cookies.Get(cookie_name);
		if (cookie == null)
		{
			cookie = new HttpCookie(cookie_name);
			HttpContext.Current.Request.Cookies.Add(cookie);
		}
		cookie.Value = cookie_value;
		HttpContext.Current.Request.Cookies.Set(cookie);
	}
    }
}